import base64_encode from "../../../utils/base64_encode";
import { Toast } from 'vant';
import cTitle from "components/title";


export default {
  data() {
    return {
      bank_name: "",
      bank_id: "",
      bank_user: "",
      bank_province: "",
      bank_city: "",
      bank_branch: "",
      result: '1',
      status: false,
      bankInfo: {},//银行卡信息
      bankMobile: "",
      bankIdcard: "",

      code_active: false,
      code_val: "",
      phone: "",

      timer: "",
      count: "",
      btnText: "发送验证码",
      btnTag: false,
      btnBg: "#ef4f4f"
    };
  },

  mounted() {
    console.log(this.$route.params);
    this.phone = this.$route.params.num;
    if (!this.$route.params.num) {
      this.$router.go(-1)
    }
  },

  methods: {

    //发送验证码
    verificationCode() {
      let that = this;
      if (this.fun.isTextEmpty(this.phone)) {
        Toast("请填写手机号");
        return;
      }
      if (this.fun.isMoblie(this.phone)) {
        Toast("请填写正确的手机号");
        return;
      } else {
        Toast.loading({
          duration: 0, // 持续展示 toast
          forbidClick: true,
          message: "发送中"
        });
        //发送获取验证码的请求
        $http
          .get("member.register.sendCodeV3", {
            mobile: this.phone,
            state: '86',
            captcha: "",
            code: this.code_val,
            sms_type: 1
          })
          .then(
            response => {
              Toast.clear();
              if (response.result == 1) {
                that.sendCodeAnimation();
              }
            },
            response => {
              console.log(response.msg + "nihao");
            }
          );
      }
    },
    //发送验证码 倒计时
    sendCodeAnimation() {
      const TIME_COUNT = 60;
      if (!this.timer) {
        this.count = TIME_COUNT;
        Toast.success("验证码已发送");
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
            this.btnText = this.count + "s后重新获取";
            this.btnTag = true;
            this.btnBg = "#999";
          } else {
            this.btnTag = false;
            this.btnBg = "#ef4f4f";
            this.btnText = "发送验证码";
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000);
      }
    },
    confirm_change() {
      let that = this;
      let yz_redirect = yz_redirect = base64_encode("");
      $http.post("member.login.index", {
        country: "86",
        mobile: this.phone,
        password: "",
        code: this.code_val,
        captcha: "",
        uuid: 0,
        mid: 0,
        is_sms:1,
        mobileErr: "",
        passwordErr: "",
        basic_info: 1,
        login_checked: false
      }, "", "", "").then(async (response) => {
        console.log(response);
        if (response.result === 1) {
          that.code_active = false
          that.status = true;
          that.bank_id = "";
        }else{
          Toast(response.msg);
        }
      })

    },
    change() {
      // this.code_active = true
      this.status = true;
      this.bank_id = "";
    },

    init() {
      this.bank_name = "";
      this.bank_id = "";
      this.bank_user = "";
      this.bank_province = "";
      this.bank_city = "";
      this.bank_branch = "";
    },

    //跳转
    login() {
      this.$router.push(this.fun.getUrl("login"));
    },


    //获取信息
    getData() {
      let json = {};
      let that = this;
      $http.post("member.bank-card.show", json, "").then((response) => {

        if (response.result == 1) {
          that.bank_name = response.data.bank_name;
          that.bank_id = response.data.bank_card;
          that.bank_user = response.data.member_name;
          that.bank_province = response.data.bank_province;
          that.bank_city = response.data.bank_city;
          that.bank_branch = response.data.bank_branch;
          this.bankInfo = response.data;
          this.result = response.data.bank_type ? response.data.bank_type.toString() : '';
          if (this.bankInfo.hasOwnProperty('mobile')) { //查询对象有没有这个属性名，后端返回字段就是开关开启，没有返回就是关闭
            this.bankMobile = response.data.mobile;
          }
          if (this.bankInfo.hasOwnProperty('idcard')) {
            this.bankIdcard = response.data.idcard;
          }
          that.status = false;
        } else {
          that.$dialog.alert({ message: response.msg });
        }
      }, (response) => {
        that.$dialog.alert({ message: response.msg });
      });
    },


    //注册
    bind() {
      let that = this;
      if (this.fun.isTextEmpty(this.bank_name)) {
        Toast({
          message: "请输入开户行",
          timeout: 1500
        });
        return;
      }

      if (this.fun.isTextEmpty(this.bank_province)) {
        Toast({
          message: "请输入开户行省份",
          timeout: 1500
        });
        return;
      }

      if (this.fun.isTextEmpty(this.bank_city)) {
        Toast({
          message: "请输入开户行城市",
          timeout: 1500
        });
        return;
      }


      if (this.fun.isTextEmpty(this.bank_branch)) {
        Toast({
          message: "请输入开户行支行",
          timeout: 1500
        });
        return;
      }
      if (this.fun.isTextEmpty(this.bank_id)) {
        Toast({
          message: "请输入账号",
          timeout: 1500
        });
        return;
      }

      if (this.fun.isTextEmpty(this.bank_user)) {
        Toast({
          message: "请输入姓名",
          timeout: 1500
        });
        return;
      }
      if (this.fun.isTextEmpty(this.bankIdcard) && this.bankInfo.hasOwnProperty('idcard')) {
        Toast({
          message: "请输入身份证",
          timeout: 1500
        });
        return;
      }
      console.log(this.bankMobile)
      console.log(this.fun.isTextEmpty(this.bankMobile));
      console.log(this.bankInfo.hasOwnProperty('mobile'));
      if (this.fun.isTextEmpty(this.bankMobile) && this.bankInfo.hasOwnProperty('mobile')) {
        Toast({
          message: "请输入银行卡预留手机号",
          timeout: 1500
        });
        return;
      }


      let json = {
        "bank_card": this.bank_id,
        "member_name": this.bank_user,
        "bank_name": this.bank_name,
        "bank_province": this.bank_province,
        "bank_city": this.bank_city,
        "bank_branch": this.bank_branch,
        "bank_type": this.result
      };
      if (this.bankInfo.hasOwnProperty('idcard')) {
        json.idcard = this.bankIdcard;
      }
      if (this.bankInfo.hasOwnProperty('mobile')) {
        json.mobile = this.bankMobile;
      }
      $http.post("member.bank-card.edit", json, "绑定中...").then((response) => {

        if (response.result == 1) {
          if (window.history.length <= 1) {
            that.$router.push(that.fun.getUrl('home', {}));
          } else {
            that.$router.go(-1);
          }
        } else {
          that.$dialog.alert({ message: response.msg });
        }
      }, (response) => {
        that.$dialog.alert({ message: response.msg });
      });
    }

  },
  activated() {
    this.init();
    this.getData();
  },
  watch: {
    bank_name(v) {
      let newVal = v.replace(/[0-9]+/g, '')
      this.bank_name = newVal.replace(/\s*/g, '');
    }
  },

  components: { cTitle }


};
